package com.xiezc.springboot_dht.entityx;

import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author wb-xzc291800
 * @date 2018/05/09 16:02
 */
public class RouterTable extends ConcurrentHashMap<Integer, Bucket> {

    public Bucket getBucket(int key) {
        return computeIfAbsent(key, k ->
            new Bucket(key)
        );
    }

    public boolean refreshAndCheckEmpty() {
        Iterator<Entry<Integer, Bucket>> iterator = entrySet().iterator();
        boolean emptyFlag = true;
        while (iterator.hasNext()) {
            Entry<Integer, Bucket> next = iterator.next();
            Bucket bucket = next.getValue();
            if (!bucket.isEmpty()) {
                if (bucket.getBk() != 160) {
                    emptyFlag = false;
                }
                bucket.refresh();
            }
        }
        return emptyFlag;
    }

}
